<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 用户列表卡片 -->
    <el-card class="box-card">
      <!-- 表单 -->
      <el-form :inline="true" :model="userVO" class="demo-form-inline">
        <el-form-item label="专业">
          <el-select v-model="userVO.departmentId" placeholder="请选择">
            <el-option
              v-for="item in departments"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
              <span style="float:left">{{item.name}}</span>
              <span style="float:right;color:#8492a6;font-size:13px">
                <span class="el-tag el-tag--success el-tag-mini el-tag--plain">{{item.deptCount}}</span>
              </span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="70px">
          <el-input v-model="userVO.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" label-width="70px">
          <el-input clearable v-model="userVO.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label-width="70px">
          <el-radio v-model="userVO.sex" label="0">帅哥</el-radio>
          <el-radio v-model="userVO.sex" label="1">美女</el-radio>
          <el-radio v-model="userVO.sex" label>全部</el-radio>
        </el-form-item>
        <el-form-item label="昵称" label-width="70px">
          <el-input clearable v-model="userVO.nickname" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <el-form-item style="marign-left: 10px">
          <el-button icon="el-icon-refresh" @click="resetUserVO">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="getUserList">查询</el-button>
          <el-button type="success" icon="el-icon-plus" @click="show">添加</el-button>
          <el-button type="warning" icon="el-icon-download">导出</el-button>
        </el-form-item>
      </el-form>
      <!-- 表格内容显示区 -->
      <el-table :data="userList" border style="width: 100%;" max-height="560">
        <el-table-column prop="id" label="编号" width="150"></el-table-column>
        <el-table-column prop="username" label="用户名" width="150"></el-table-column>
        <el-table-column prop="sex" label="性别" width="110">
          <template slot-scope="scope">
            <span
              class="el-tag el-tag--success el-tag--mini el-tag--light"
            >{{scope.row.sex==0?'帅哥':(scope.row.sex==1?'美女':'保密')}}</span>
          </template>
        </el-table-column>
        <el-table-column sortable prop="name" label="所属部门" width="180"></el-table-column>
        <el-table-column sortable prop="birth" label="生日" width="180"></el-table-column>
        <el-table-column sortable prop="email" label="邮箱" width="180"></el-table-column>
        <el-table-column prop="phoneNumber" label="电话" width="150"></el-table-column>
        <el-table-column prop="status" label="是否禁用" width="100">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" active-color="#ff4949" inactive-color="#13ce66"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
          <el-button type="warning" size="mini" icon="el-icon-s-tools"></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          style="padding-top:15px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[8, 10, 20, 30]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
      <UserAdd
        :departmentList="departments"
        :addOrUpdateVisible="addOrUpdateVisible"
        @changeShow="showAddOrUpdate"
        ref="addOrUpdateRef"
      ></UserAdd>
    </el-card>
  </div>
</template>

<script>
// 引用后台接口方法
import { findUserList } from "../../api/user.js";
import { findDeptAndCount } from "../../api/department.js";

import UserAdd from "./UserAdd";

export default {
  name: "Users",
  data() {
    return {
      userVO: {
        departmentId: "",
        email: "",
        username: "",
        sex: "",
        nickname: ""
      },
      // 用户集合
      userList: [],
      // 每页显示的条数
      size: 8,
      // 总条数
      total: 100,
      // 当前页码
      current: 1,
      // 部门集合
      departments: [],
      // 弹窗控制键
      addOrUpdateVisible: false
    };
  },
  created() {
    // 创建组件的时候调用查询所有用户的方法
    this.getUserList();
    this.getDeptAndCount();
  },
  methods: {
    // 重置表单
    resetUserVO() {
      this.userVO.departmentId = "";
      (this.userVO.email = ""),
        (this.userVO.username = ""),
        (this.userVO.sex = ""),
        (this.userVO.nickname = ""),
        this.getUserList();
    },
    // 当每页条数改变的时候
    handleSizeChange(val) {
      // 将val赋值给size
      this.size = val;
      // 重新去后台查询数据
      this.getUserList();
    },
    // 当前页码改变的时候
    handleCurrentChange(val) {
      this.current = val;
      this.getUserList();
    },

    // 显示弹框
    show() {
      this.addOrUpdateVisible = true;
    },
    // 监听子组件弹窗关闭后触发，有子组件调用
    showAddOrUpdate(data) {
      if (data === "false") {
        this.addOrUpdateVisible = false;
      } else {
        this.addOrUpdateVisible = true;
      }
    },

    // 从后台接受数据
    async getUserList() {
      const { data } = await findUserList(this.current, this.size, this.userVO);
      this.userList = data.data.records;
      this.total = data.data.total;
    },
    async getDeptAndCount() {
      const { data } = await findDeptAndCount();
      this.departments = data.data.departments;
    }
  },
  watch:{
    sex(){
        this.getUserList();
    },
    department(){
      this.getUserList();
    }
  },
  computed:{
    sex(){
      return this.userVO.sex
    },
    department(){
      return this.userVO.departmentId
    }
  },
  components: {
    UserAdd
  }
};
</script>

<style scpoed>
.box-card {
  margin-top: 20px;
}
</style>
